Page({
  data: {
    list: [] as string[],
    isRefresh: false
  },
  testRequest(){
    wx.request({
      url: 'https://api-vue-base.itheima.net/api/joke',
      method: 'GET',
      success: (res)=>{
        console.log(res.data);
      },
      fail: ()=>{},
      complete: ()=>{}
    })
  },
  getJokeList(){
    return new Promise((resolve: (value: string[]) => void,reject)=>{
      wx.showLoading({
        title: '数据加载中',
      })
      wx.request<any>({
        url: 'https://api-vue-base.itheima.net/api/joke/list',
        method: 'GET',
        data:{ num: 10 }, // 传参
        success: (res)=>{
          console.log(res.data);
          // this.setData({
          //   list: res.data.data
          // })
          resolve(res.data.data)
        },
        fail: ()=>{
          reject()
        },
        complete: ()=>{
          wx.hideLoading()
        }
      })
    })
  },
  // 页面加载
  onLoad(){
    this.getJokeList().then((res)=>{
      this.setData({
        list: res
      })
    })
  },
  // 页面卸载
  onUnload(){},
  // 页面显示
  onHide(){},
  // 页面隐藏
  onShow(){},
  async reloadData(){
    this.setData({
      isRefresh: true
    })
    await this.getJokeList()
    // setTimeout(()=>{
    //   this.setData({
    //     isRefresh: false
    //   },3000)
    // })
    this.setData({ isRefresh:false })
  },
  async getMoreData(){
    const res = await this.getJokeList()
    // console.log(res);
    this.data.list.push(...res)
    this.setData({
      list: this.data.list
    })
  }
})